//
//  TagsViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/11/4.
//  Copyright © 2018 wjk. All rights reserved.
//

import UIKit


class TagsViewController: BaseViewController {
    
    
    var bl:changeUserBlock?
    
    var data:[String:Any] = ["key":"","value":""]
    
    var routerData = [[String:Any]()]
    
    var tagView:SQButtonTagView!
    
    var selectArr:NSArray = NSArray()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.tagView = SQButtonTagView.init(totalTagNum: 30, viewWidth: deviceWidth()-20, eachNum: 0, hmargin: 10, vmargin: 10, tagheight: 30, tagTextFont: UIFont.systemFont(ofSize: 14), tagTextColor: UIColor.red.withAlphaComponent(0.5), selectedTagTextColor: UIColor.white, selectedBackgroundColor: UIColor.red.withAlphaComponent(0.5))
        self.tagView?.maxSelectNum = 100
        self.view.addSubview(self.tagView)
        self.tagView.selectBlockAction { (array) in
            self.selectArr = array
        }
        
        let data2:NSMutableArray = NSMutableArray()
        
        routerData.forEach { (d) in
            data2.add(d["name"] as! String)
        }
        self.tagView.setTagTexts(tagTexts: data2 as! Array<Any>)
        
//        self.tagView.selectAll(self)
        
        self.tagView.translatesAutoresizingMaskIntoConstraints = false
        self.tagView.frame = CGRect.init(x: 10, y: 30, width: deviceWidth()-20, height: 300)
        // Do any additional setup after loading the view.
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "保存", style: .plain, target: self, action: #selector(self.save(_:)))
        
        if jsonFormater(data: self.data)["value"].stringValue.isEmpty == false {
            self.tagView.setSelectTags(tag: (self.data["value"] as! String).split(separator: ","))
        }
        
        
        
    }
    
    @objc func save(_ bar : UIBarButtonItem) {
        //        self.data["value"] = self.tfInput.text!
        
        if self.selectArr.count <= 0 {
            showMessage(message: "请选择企业类型")
            return
        }
        
        
        let data2:NSMutableArray = NSMutableArray()
        self.selectArr.forEach { (i) in
            data2.add(self.routerData[i as! Int]["name"] as! String)
        }
        
        self.bl?(["key":self.data["key"] as! String,"value":data2.componentsJoined(by: ",")])
        self.navigationController?.popViewController(animated: true)
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
